package com.dream.service.user.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.toolkits.cache.RedisCache;
import com.dream.service.user.api.TelCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.Random;

public class TelCodeServiceImpl implements TelCodeService {

    private  static RedisCache redisCache = RedisCache.getInstance("user_redis_address");
    //验证码4位
    private int loop = 4;
    private String default_charset = "utf-8";
    private String prefix = "telCode_";
    int expire = 300;
    private Logger logger = LoggerFactory.getLogger(TelCodeServiceImpl.class);
    @Override
    public BaseResponse<Boolean> sendCode(String tel) {
        BaseResponse<Boolean> baseResponse = new BaseResponse<Boolean>();
        if(!validTel(tel)){
            baseResponse.packData(true, ErrorCode.PARAM_ERROR.getCode(), "unvalid tel", null, null);
            return baseResponse;
        }
        String validCode = generateCode(loop);
        try {
            redisCache.setToCache(prefix.concat(tel).getBytes(default_charset),validCode.getBytes(default_charset));
            redisCache.setExpire(expire);
        } catch (UnsupportedEncodingException e) {
            baseResponse.setResult(false);
        }
        baseResponse.setSuccess(true);
        baseResponse.setResult(true);
        baseResponse.setError_msg(validCode);
        return baseResponse;
    }

    @Override
    public BaseResponse<Boolean> validCode(String tel, String code) {
        BaseResponse<Boolean> baseResponse = new BaseResponse<Boolean>();
        if(StringUtils.isEmpty(tel) || StringUtils.isEmpty(code)){
            baseResponse.packData(true, ErrorCode.PARAM_ERROR.getCode(), "unvalid tel or code", null, null);
            return baseResponse;
        }
        try {
           byte[] value = redisCache.getFromCache(prefix.concat(tel).getBytes(default_charset));
           if(value == null){
               baseResponse.packData(true, ErrorCode.PARAM_ERROR.getCode(), "unvalid tel or code", null, null);
               return baseResponse;
           }
           String realCode = new String(value);
           if(!code.equals(realCode)){
               baseResponse.packData(true, ErrorCode.PARAM_ERROR.getCode(), "please check your code", null, null);
           }else{
               baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
           }
        } catch (Exception e) {
            logger.error("process validCode error:{}",e);
            baseResponse.packData(true, ErrorCode.PROCESS_ERROR.getCode(), "process error", null, null);
        }
        return baseResponse;
    }

    public String generateCode(int loop){
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<loop;i++){
            int tmp = random.nextInt(10);
            stringBuilder.append(tmp);
        }
        return stringBuilder.toString();
    }

    public void sendMessage(String tel,String code){
        String message = "you register code:"+code;
        //send
    }

    /**
     * 验证tel
     * @param tel
     * @return
     */
    private boolean validTel(String tel){
        return !StringUtils.isEmpty(tel);
    }
}
